<template>
    <div class="app-container">
        <el-dialog :title="title" :visible.sync="visible" width="1000px" v-if="visible" append-to-body
            :close-on-click-modal="false">
            <p class="refuse_txt" v-if="status == 'reupload'">拒绝原因：{{ refuseForm.refuseValue }}</p>
            <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :disabled="disabled"
                v-loading="loading">
                <el-form-item label="合同内容：" prop="">
                    <ImageUploadMore :limit="10" :fileSize="20" :previewType="'2'" @successImg="getContractImgList"
                        :value="form.contractImgList" :isShowTip="status == 'watch' ? false : true">
                    </ImageUploadMore>
                </el-form-item>
                <el-form-item label="电子合同：" prop="">
                    <div class="file_box">
                        <el-upload class="upload-demo" :action="fileaction" :headers="headers" :data="fileData" :limit="1"
                            :on-success="handleFileChange" :on-remove="handleRemove" :show-file-list="true"
                            :on-exceed="handleExceed" :on-preview="handlePictureCardPreview" :file-list="fileList">
                            <el-button type="primary" size="small">上传电子合同</el-button>
                            <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
                        </el-upload>
                    </div>

                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" :loading="sumitLoading" @click="submitForm"
                    v-if="status == 'upload'||status == 'modify'">提交审核</el-button>
                <el-button type="primary" :loading="sumitLoading" @click="approved"
                    v-if="status == 'examine'">审核通过</el-button>
                <el-button type="danger" :loading="sumitLoading" @click="refuseReview"
                    v-if="status == 'examine'">拒绝审核</el-button>
                <el-button type="primary" :loading="sumitLoading" v-if="status == 'reupload'"
                    @click="submitForm">重新提交</el-button>
                <el-button @click="cancel">关闭</el-button>
            </div>
            <!-- 导入失败弹窗 -->
            <el-dialog title="导入失败" :visible.sync="dialogVisible" width="500" append-to-body>
                <div>{{ importMessage }}</div>
                <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 拒绝弹窗 -->
            <el-dialog title="确认审核" :visible.sync="refuseVisible" width="500" append-to-body :close-on-click-modal="false">
                <div class="refuse_box">是否拒绝通过 <span style="color: red;">{{ shopName }}</span> 的合同审批商家发行的全部赚卡将同步下架</div>
                <el-form ref="formRefuse" :model="refuseForm" :rules="rules" label-width="100px" :disabled="disabled">
                    <el-form-item label="拒绝原因：" prop="refuseValue">
                        <el-input v-model="refuseForm.refuseValue" maxlength="255" placeholder="请输入拒绝原因"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="refuseSubmit" :loading="sumitLoading">确 定</el-button>
                    <el-button @click="refuseClose">取 消</el-button>
                </span>
            </el-dialog>
        </el-dialog>
    </div>
</template>
  
<script>
import {
    getToken
} from "@/utils/auth";
//api
import {
    contractDetail, contractModify, contractExamine
} from "@/api/merchants/contractReview";

export default {
    // dicts: ['asset_card_type'],
    name: "Dialog",
    props: {

    },
    data() {
        return {
            labelWidth: '150px',
            inputWidth: '320px',
            dialogVisible: false, //导入失败弹窗
            loading: false,
            sumitLoading: false,
            importMessage: '', //导入失败原因
            id: '', //
            status: '',//合同状态
            // 是否禁用表单
            disabled: false,
            // 弹出层标题
            title: "合同",
            shopName: '',//店铺名称
            // 是否显示弹出层
            visible: false,
            // 导入所需参数
            addLoading: false,
            fileName: '',//电子合同文件名
            fileData: {},
            fileList: [],
            fileaction: process.env.VUE_APP_BASE_API + "/common/v1/upload", //导入接口
            headers: {}, //设置上传文件请求头
            tabelList: [],
            isImplement: false, //是否执行
            phone: '',
            // 表单参数
            form: {
                contractFileName: '',//文件名
                contractFile: '',//合同文件地址
                contractImgList: [],//合同内容
                id: '',//
            },
            refuseVisible: false,
            refuseForm: {
                refuseValue: ''
            },
            // 表单校验
            rules: {
                refuseValue: [{
                    required: true,
                    message: '请输入拒绝原因',
                    trigger: ['change', 'blur']
                }],
            },
        };
    },
    created() {

    },
    methods: {
        //查询信息详情
        getInfo(id) {
            this.loading = true
            contractDetail(id).then(res => {
                this.form.contractFile = res.result.contractFile
                this.fileName = res.result.contractFileName
                if (res.result.contractFile != '') {
                    this.fileList.push({
                        name: this.fileName,
                        url: this.form.contractFile
                    });
                }
                this.form.contractImgList = res.result.contractImgList
                this.refuseForm.refuseValue = res.result.reason
                this.form.id = res.result.id
                setTimeout(() => {
                    this.loading = false
                }, 500)
            }).catch(() => {
                this.loading = false
            })
        },
        /** 打开弹框 */
        open(id, status, shopName) {
            this.reset()
            console.log('status', status)
            this.status = status
            this.shopName = shopName
            if (status == 'upload') {
                this.title = '上传合同'
            } else if (status == 'modify') {
                this.title = '修改合同'
            } else if (status == 'reupload') {
                this.title = '重新上传'
            } else if (status == 'examine') {
                this.title = '审批合同'
            } else if (status == 'watch') {
                this.title = '查看合同'
                this.disabled = true
            }
            this.getInfo(id)
            this.form.id = id
            this.visible = true
        },
        // 取消按钮
        cancel() {
            this.visible = false;
            // this.reset();
        },
        // 重置
        reset() {
            this.refuseVisible = false
            this.sumitLoading = false
            this.refuseForm = {
                refuseValue: ''
            }
            this.disabled = false
            this.fileList = []
        },
        //获取图片
        getContractImgList(e) {
            if (e != '') {
                this.form.contractImgList = e.split(",")
                // this.$refs['ruleForm'].clearValidate(['pictureList']);
            } else {
                this.form.contractImgList = []
            }
        },
        //导入文件
        handleFileChange(res, file) {
            console.log('文件', res, file)
            if (res.code == 200) {
                this.$message.success('上传成功');
                this.form.contractFile = res.result.url
                this.form.contractFileName = file.name
                // this.getCustomerLs(this.id)
            } else {
                // this.$message.error(res.message);
                this.$message.error('上传失败')
                this.importMessage = res.message || res.msg
                this.dialogVisible = true
                setTimeout(() => {
                    this.fileList = []
                }, 800)
            }
        },
        //删除文件
        handleRemove() {
            this.form.contractFile = ''
            this.form.contractFileName = ''
            this.fileList = []
        },
        // 文件个数超出
        handleExceed() {
            this.$message.error(`上传文件数量不能超过 1 个,请先删除原有文件!`);
        },
        // 预览
        handlePictureCardPreview(file) {
            window.open(this.form.contractFile)
            // if (this.previewType == 1) {
            //     this.dialogImageUrl = file.url;
            //     this.dialogVisible = true;
            // } else {
            //     let arrnew = [];
            //     this.fileList.forEach((e, index) => {
            //         if (e.url == file.url) {
            //             this.initialIndex = index
            //         }
            //         arrnew.push(e.url)
            //     })
            //     this.srcList = arrnew
            //     this.openImg()
            // }
        },
        //审核通过
        approved() {
            if (this.form.contractFile == '' && this.form.contractImgList.length == 0) {
                this.$message.error('电子合同和合同内容不能都为空')
                return
            }
            this.$confirm('是否确认通过 ' + this.shopName + ' 的合同审批?', '确认审核', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // this.sumitLoading = true
                let data = {
                    status: '3',
                    ...this.form
                }
                contractExamine(data).then(response => {
                    this.$message.success('执行成功');
                    this.cancel();
                    this.$emit('success')
                }).catch(() => {
                    // this.sumitLoading = false;
                });
            }).catch(() => { });
        },
        //
        refuseClose() {
            this.refuseForm = {
                refuseValue: ''
            }
            setTimeout(() => {
                this.$refs['formRefuse'].clearValidate(['refuseValue'])
            }, 500)
            this.refuseVisible = false
        },
        //拒绝审核
        refuseReview() {
            this.refuseVisible = true
        },
        //拒绝审核提交
        refuseSubmit() {
            this.$refs["formRefuse"].validate(valid => {
                if (valid) {
                    let data = {
                        status: '4',
                        ...this.form,
                        reason: this.refuseForm.refuseValue
                    }
                    this.sumitLoading = true
                    contractExamine(data).then(response => {
                        this.$message.success('执行成功');
                        this.cancel();
                        this.$emit('success')
                    }).catch(() => {
                        this.sumitLoading = false;
                    });
                }
            });
        },
        /** 提交审核按钮 */
        submitForm() {
            this.sumitLoading = true
            contractModify(this.form).then(response => {
                this.$message.success('执行成功');
                this.cancel();
                this.$emit('success')
            }).catch(() => {
                this.sumitLoading = false;
            });
            // this.$refs["form"].validate(valid => {
            //     if (valid) {
            //         this.sumitLoading = true
            //         this.form.applyId = this.id
            //         addApplyIssueExecute(this.form).then(response => {
            //             this.$message.success('执行成功');
            //             this.cancel();
            //             this.$emit('success')
            //         }).catch(() => {
            //             this.sumitLoading = false;
            //         });
            //     }
            // });
        },
    }
};

</script>
<style scoped>
.main {
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
}

.icon_yes {
    font-size: 28px;
    color: #67c23a;
}

.img_list {
    display: flex;
    flex-wrap: wrap;
}

.img_list_box {
    display: flex;
    flex-wrap: wrap;
}

.img_list_box_demo {
    margin-right: 20px;
    margin-bottom: 20rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20px;
}

.demo_image_preview {
    border: 1px dashed #d9d9d9;
    width: 120px;
    height: 120px;
    margin-bottom: 20px;
}


.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
    border: 1px dashed #d9d9d9;
}

.refuse_box {
    margin: 0 0 20px 20px;
}

::v-deep .file_box .el-upload-list__item {
    width: 50%;
}

.refuse_txt {
    font-size: 15px;
    font-weight: bold;
    color: red;
    margin-bottom: 30px;
}
</style>
  